package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.ListViewForScrollView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.car.MyActivity.OnthedoorActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.TotalEvaluationAdapter;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.BuyACarFragmentModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleaningActivity.CLEANING_BEAN;

/**
 * Created by Administrator on 2017/8/30.
 * 0、苦力  1、开锁 2、上户 店铺详情
 */

public class CoolieActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.shop_evaluation_listview)
    ListViewForScrollView shopEvaluationListview;
    List<BuyACarFragmentModel> shopEvaluationlist;
    TotalEvaluationAdapter evaluationAdapter;
    @Bind(R.id.all_evaluation)
    TextView allEvaluation;
    @Bind(R.id.contact)
    TextView contact;
    @Bind(R.id.ad_pager)
    ViewPager adPager;
    @Bind(R.id.ad_layer)
    LinearLayout adLayer;
    @Bind(R.id.merchant_name)
    TextView mMerchant;
    @Bind(R.id.interduce)
    TextView mInterduce;
    @Bind(R.id.address)
    TextView mAddress;
    @Bind(R.id.rating)
    RatingBar mRat;
    @Bind(R.id.order_count)
    TextView mOrderCount;
    @Bind(R.id.otal_evaluation_text)
    TextView mOtalEvalua;
    @Bind(R.id.merchant_name_bottom)
    TextView mMerchantNameBottom;
    @Bind(R.id.address_bottom)
    TextView mAddressBottom;
    private CallOrderBean mBean;
    private final int HOME_AD_RESULT = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }

        ;
    };
    Context context;
    String pos = "0";
    private String mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coolie);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        contact.setOnClickListener(this);
        allEvaluation.setText(Html.fromHtml("查看全部评价<font color='#FF4537'>(157)</font>"));
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
            mBean = (CallOrderBean) getIntent().getSerializableExtra(CLEANING_BEAN);
        }
        if (pos.equals("0")) {
            contact.setText("呼叫苦力");
            mApi = Constant.UNLOCKING_CALLOUT;
        } else if (pos.equals("1")) {
            contact.setText("呼叫开锁");
            mApi = Constant.UNLOCKING_CALLOUT;
        } else if (pos.equals("2")) {
            contact.setText("呼叫上户");
            mApi = Constant.UNLOCKING_CALLOUT;
        }
        if (mBean.figurepic.length() > 0) {
            List<String> pic = new ArrayList<>();
            String[] split = mBean.figurepic.split("\\|");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                pic.add(i, s);
            }
            getadvertising(pic.size(), pic);
        }
        evaluationAdapter = new TotalEvaluationAdapter(this);
        shopEvaluationListview.setAdapter(evaluationAdapter);
        LoadData();
    }

    private void LoadData() {
        mMerchant.setText(mBean.name);
        mInterduce.setText(mBean.profile);
        mAddress.setText(mBean.suite);
        mRat.setStar((float) mBean.star);
        mOrderCount.setText(String.valueOf(mBean.order));
        mMerchantNameBottom.setText(mBean.name);
        mAddressBottom.setText(mBean.suite);
        // mOtalEvalua.setText(mBean.countcomment);
    }

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.contact:
                if (pos.equals("0")) {//苦力
                    startActivity(new Intent(context, CallCoolieActivity.class));
                } else if (pos.equals("1")) {//开锁
                    startActivity(new Intent(context, CallLockActivity.class));
                } else if (pos.equals("2")) {//上户
                    startActivity(new Intent(context, OnthedoorActivity.class));
                }
                break;

        }
    }
}
